package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.button;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.MenuButtonMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Menu;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.MenuButton;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.button.JsmMenuButtonService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author 卢曼成
 * @Description 资源按钮逻辑层
 * @Date 2017/2/20 17:20
 */

public class JsmMenuButtonServiceImpl implements JsmMenuButtonService {
    @Autowired
    private MenuButtonMapper menuButtonMapper;

    /**
     * 根据ID删除
     *
     * @param menuFuncId
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String menuFuncId) {
        return menuButtonMapper.deleteByPrimaryKey(menuFuncId);
    }

    /**
     * 添加方法
     *
     * @param record
     * @return
     */
    @Override
    public int insert(MenuButton record) {
        return menuButtonMapper.insert(record);
    }

    /**
     * 非空添加
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(MenuButton record) {
        return menuButtonMapper.insertSelective(record);
    }

    /**
     * 根据ID查询
     *
     * @param menuFuncId
     * @return
     */
    @Override
    public MenuButton selectByPrimaryKey(String menuFuncId) {
        return menuButtonMapper.selectByPrimaryKey(menuFuncId);
    }

    /**
     * 非空修改
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(MenuButton record) {
        return menuButtonMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 修改
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKey(MenuButton record) {
        return menuButtonMapper.updateByPrimaryKey(record);
    }

    /**
     * @Author 卢曼成
     * @Description 批量添加
     * @Date 2017/2/8 17:17
     */
    @Override
    public int insertList(Menu menu, Employee employee) {
        int resInt = 0;
        if (StringUtils.isNotEmpty(menu.getButtonList())) {
            List<MenuButton> menuButtonList = JSON.parseArray(menu.getButtonList(), MenuButton.class);
            for (MenuButton menuButton : menuButtonList) {
                // 更新 或者 删除
                if (StringUtils.isEmpty(menuButton.getButtonId())) {
                    String funcId = UniqueUtil.uuid();
                    menuButton.setMenuFuncId(funcId);
                    menuButton.setButtonId(funcId);
                    menuButton.setStatus(BigDecimal.valueOf(0));
                    menuButton.setMenuId(menu.getMenuId());
                    menuButton.setCreator(employee.getEmployeeId());
                    menuButton.setCreatetime(new Date());
                    menuButton.setModifyer(employee.getEmployeeId());
                    menuButton.setModifytime(new Date());
                    menuButton.setModifyer(employee.getEmployeeId());
                    resInt = resInt + menuButtonMapper.insertSelective(menuButton);
                } else {
                    menuButton.setModifyer(employee.getEmployeeId());
                    menuButton.setModifytime(new Date());
                    resInt = resInt + menuButtonMapper.updateByPrimaryKeySelective(menuButton);
                }
            }
        }
        // 删除按钮
        if (StringUtils.isNotEmpty(menu.getDelBtnList())) {
            List<String> delBtnList = JSON.parseArray(menu.getDelBtnList(), String.class);
            if (delBtnList != null && delBtnList.size() > 0) {
                for (String delBtnId : delBtnList) {
                    menuButtonMapper.deleteByPrimaryKey(delBtnId);
                }
            }

        }
        return resInt;
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量修改
     * @Date 2017/2/8 17:17
     */
    @Override
    public int updateList(List<MenuButton> list) {
        return menuButtonMapper.updateList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量逻辑删除
     * @Date 2017/2/8 17:17
     */
    @Override
    public int deleteList(List<MenuButton> list) {
        return menuButtonMapper.deleteList(list);
    }

    /**
     * 根据条件查询ID集合
     *
     * @param list
     * @return
     */
    @Override
    public List<String> selectByListId(List<MenuButton> list) {
        return menuButtonMapper.selectByListId(list);
    }

    /**
     * 查询菜单按钮
     *
     * @param menuButton
     * @return
     */
    @Override
    public JSONObject ajaxQueryButtonList(MenuButton menuButton) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", menuButtonMapper.queryMenuButtonCount(menuButton));
        jsonObject.put("rows", JSON.toJSON(menuButtonMapper.queryMenuButtonList(menuButton)));
        return jsonObject;
    }
}
